package com.blb.thread;

/**
 * 后台线程
 */
public class DaemonThreadDemo {

    public static void main(String[] args) {
        Thread myThread = new Thread(() -> {
           while (true){
               try {
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
               System.out.println(Thread.currentThread().getName() + "为你服务");
           }
        });
        Thread thread1 = new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "-->" + i);
            }
        });
        Thread thread2 = new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "-->" + i);
            }
        });
        //设置为后台线程
        myThread.setDaemon(true);
        myThread.start();
        thread1.start();
        thread2.start();
    }
}
